/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SetScaleDialog.java
 *
 * Created on 2011-07-11, 21:32:26
 */
package JFreeChartExtensions.ChartPanelDialogs;

import JFreeChartExtensions.chartextensions.inset.ChartInsetManager;
import JFreeChartExtensions.chartextensions.utils.Inset;
import JFreeChartExtensions.chartextensions.utils.InsetChartPanel;
import JFreeChartExtensions.ExtendableChartPanel;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import javax.swing.JOptionPane;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;

/**
 *
 * @author Nobody
 */
public class InsetSettings extends ChartPanelDialog {

    private InsetChartPanel inset;
    private Inset insetWindow;
    private ChartInsetManager chartInsetManager;
    static boolean isVisible = true;

    public InsetSettings(ExtendableChartPanel chartPanel, ChartInsetManager insetManager) {
        super(chartPanel);
        initComponents();
        this.chartInsetManager = insetManager;
        this.inset = chartInsetManager.getInsetChartPanel();
        this.insetWindow = chartInsetManager.getInset();
        Rectangle dim = inset.getBounds();


        XYPlot plot = parentPanel.getXYPlot();
        ValueAxis domainAxis = plot.getDomainAxis();
        ValueAxis rangeAxis = plot.getRangeAxis();

        int xStart = (int) domainAxis.java2DToValue(dim.x, chartPanel.getScreenDataArea(), plot.getDomainAxisEdge());
        int yStart = (int) rangeAxis.java2DToValue(dim.y, chartPanel.getScreenDataArea(), plot.getRangeAxisEdge());
        int xEnd = (int) domainAxis.java2DToValue(dim.width + dim.x, chartPanel.getScreenDataArea(), plot.getDomainAxisEdge());
        int yEnd = (int) rangeAxis.java2DToValue(dim.y + dim.height, chartPanel.getScreenDataArea(), plot.getRangeAxisEdge());


        heightTextField.setText(Integer.toString(yEnd));
        widthTextField.setText(Integer.toString(xEnd));

        xTextField.setText(Integer.toString(xStart));
        yTextField.setText(Integer.toString(yStart));
        VisibleCheckBox.setSelected(isVisible);
        ShowFitSeriesCheckBox.setSelected(chartInsetManager.isShowFitSeries());


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        XAxisButtonGroup = new javax.swing.ButtonGroup();
        YAxisButtonGroup = new javax.swing.ButtonGroup();
        mainPanel = new javax.swing.JPanel();
        Size = new javax.swing.JPanel();
        height = new javax.swing.JLabel();
        width = new javax.swing.JLabel();
        heightTextField = new javax.swing.JTextField();
        widthTextField = new javax.swing.JTextField();
        height1 = new javax.swing.JLabel();
        width1 = new javax.swing.JLabel();
        yTextField = new javax.swing.JTextField();
        xTextField = new javax.swing.JTextField();
        VisibleCheckBox = new javax.swing.JCheckBox();
        ShowFitSeriesCheckBox = new javax.swing.JCheckBox();
        buttonPanel = new javax.swing.JPanel();
        OKButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        Size.setBorder(javax.swing.BorderFactory.createTitledBorder("Dimension"));

        height.setText("Y end : ");

        width.setText("X end :");

        height1.setText("X  start :");

        width1.setText("Y start :");

        javax.swing.GroupLayout SizeLayout = new javax.swing.GroupLayout(Size);
        Size.setLayout(SizeLayout);
        SizeLayout.setHorizontalGroup(
            SizeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SizeLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(SizeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(height1)
                    .addComponent(width1)
                    .addComponent(width)
                    .addComponent(height))
                .addGap(18, 18, 18)
                .addGroup(SizeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(SizeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(yTextField)
                        .addComponent(xTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE))
                    .addGroup(SizeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(widthTextField)
                        .addComponent(heightTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        SizeLayout.setVerticalGroup(
            SizeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SizeLayout.createSequentialGroup()
                .addGroup(SizeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(height1)
                    .addComponent(xTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(SizeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(yTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(width1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(SizeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(heightTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(height))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(SizeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(widthTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(width))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        VisibleCheckBox.setText("Visible");

        ShowFitSeriesCheckBox.setText("Show Fit Series");

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(VisibleCheckBox, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ShowFitSeriesCheckBox))
                .addContainerGap(66, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(Size, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Size, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(VisibleCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ShowFitSeriesCheckBox)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        OKButton.setText("OK");
        OKButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                OKButtonMouseClicked(evt);
            }
        });
        buttonPanel.add(OKButton);

        CancelButton.setText("Cancel");
        CancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CancelButtonMouseClicked(evt);
            }
        });
        buttonPanel.add(CancelButton);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void OKButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_OKButtonMouseClicked

        try {
            double height = Double.parseDouble(heightTextField.getText());
            double width = Double.parseDouble(widthTextField.getText());
            double x = Double.parseDouble(xTextField.getText());
            double y = Double.parseDouble(yTextField.getText());

            XYPlot plot = parentPanel.getXYPlot();
            ValueAxis domainAxis = plot.getDomainAxis();
            ValueAxis rangeAxis = plot.getRangeAxis();
            Rectangle2D screenArea = parentPanel.getScreenDataArea();

            int xStart = (int) domainAxis.valueToJava2D(x, screenArea, plot.getDomainAxisEdge());
            int yStart = (int) rangeAxis.valueToJava2D(y, screenArea, plot.getRangeAxisEdge());
            int xEnd = (int) domainAxis.valueToJava2D(width, screenArea, plot.getDomainAxisEdge());
            int yEnd = (int) rangeAxis.valueToJava2D(height, screenArea, plot.getRangeAxisEdge());

            chartInsetManager.setShowFitSeries(ShowFitSeriesCheckBox.isSelected());
            Rectangle rect = inset.getBounds();
            inset.setLastBounds(x, y, width, height);
            rect.x = xStart;
            rect.y = yStart;
            rect.width = Math.abs(xEnd - xStart) + 1;
            rect.height = Math.abs(yEnd - yStart) + 1;
            inset.setBounds(rect);
            inset.repaint();
            inset.getParent().repaint();
            if (isVisible == true && VisibleCheckBox.isSelected() == false) {
                insetWindow.setVisible(false);
                isVisible = false;
                insetWindow.updateUI();

            } else if (isVisible == false && VisibleCheckBox.isSelected() == true) {
                insetWindow.setVisible(true);
                isVisible = true;
            } else {
                insetWindow.revalidate();
            }

        } catch (NumberFormatException e) {
            throw new RuntimeException(e.getMessage());

        } catch (RuntimeException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }

        dispose();
    }//GEN-LAST:event_OKButtonMouseClicked

    private void CancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CancelButtonMouseClicked
        dispose();
    }//GEN-LAST:event_CancelButtonMouseClicked

    /*@Override
    public void dispose() {
        frameWithChart.setEnabled(true);
        super.dispose();
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JButton OKButton;
    private javax.swing.JCheckBox ShowFitSeriesCheckBox;
    private javax.swing.JPanel Size;
    private javax.swing.JCheckBox VisibleCheckBox;
    private javax.swing.ButtonGroup XAxisButtonGroup;
    private javax.swing.ButtonGroup YAxisButtonGroup;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JLabel height;
    private javax.swing.JLabel height1;
    private javax.swing.JTextField heightTextField;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JLabel width;
    private javax.swing.JLabel width1;
    private javax.swing.JTextField widthTextField;
    private javax.swing.JTextField xTextField;
    private javax.swing.JTextField yTextField;
    // End of variables declaration//GEN-END:variables
}
